package it.cums.core.util;

import it.cums.core.Activator;
import it.cums.core.pref.PreferenceConstants;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
	private static final SessionFactory sessionFactory;

	static {
		try {
			Configuration cfg = new Configuration();
			cfg.addClass(it.cums.core.model.Product.class);
			cfg.addClass(it.cums.core.model.Country.class);
			cfg.addClass(it.cums.core.model.Customer.class);
			cfg.addClass(it.cums.core.model.Supplier.class);
			cfg.addClass(it.cums.core.model.Order.class);
			cfg.addClass(it.cums.core.model.Invoice.class);
			cfg.addClass(it.cums.core.model.OrderedProduct.class);
			String dbms = Activator.getDefault().getPreferenceStore().getString(PreferenceConstants.DBMS);
			String driver = DBConfiguration.getDriver(dbms);
			String dialect = DBConfiguration.getDialect(dbms);
			String hostname = Activator.getDefault().getPreferenceStore().getString(PreferenceConstants.HOSTNAME);
			String database = Activator.getDefault().getPreferenceStore().getString(PreferenceConstants.DB);
			String port = Activator.getDefault().getPreferenceStore().getString(PreferenceConstants.PORT);
			String url = "jdbc:" + dbms.toLowerCase() + "://" + hostname + ":" + port + "/" + database;
			String username = Activator.getDefault().getPreferenceStore().getString(PreferenceConstants.USERNAME);
			String password = Activator.getDefault().getPreferenceStore().getString(PreferenceConstants.PASSWORD);
			cfg.setProperty("hibernate.dialect", dialect);
			cfg.setProperty("hibernate.connection.driver_class", driver);
			cfg.setProperty("hibernate.connection.url", url);
			cfg.setProperty("hibernate.connection.username", username);
			cfg.setProperty("hibernate.connection.password", password);
			cfg.setProperty("hibernate.connection.pool_size", "1");
			cfg.setProperty("hibernate.current_session_context_class", "thread");
			cfg.setProperty("hibernate.cache.provider_class", "org.hibernate.cache.NoCacheProvider");
			cfg.setProperty("hibernate.show_sql", "true");
			cfg.setProperty("hibernate.hbm2ddl.auto", "update");
			sessionFactory = cfg.buildSessionFactory();
			System.out.println(driver);
//			sessionFactory = new Configuration().configure().buildSessionFactory();
		} catch (Throwable ex) {
			// Make sure you log the exception, as it might be swallowed
			System.err.println("Initial SessionFactory creation failed." + ex);
			throw new ExceptionInInitializerError(ex);
		}
	}

	public static SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	
	public static void closeSessionFactory() {
		try {
			sessionFactory.close();
		} catch (Exception e) {
			Activator.getDefault().logError(e.getMessage(), e);
		}
	}
	
	public static void closeSession(Session s) {
		try {
			if (s != null) {
				s.close();
			}
		} catch (Exception e) {
			Activator.getDefault().logError(e.getMessage(), e);
		}
	}
	
	public static void rollback(Transaction tx) {
		try {
			if (tx != null) {
				tx.rollback();
			}
		} catch (Exception e) {
			Activator.getDefault().logError(e.getMessage(), e);
		}
	}
	
	public static void testConnection() throws Exception {
		Session session = null;
		try {
			session = sessionFactory.openSession();
			session.beginTransaction();
			session.getTransaction().commit();
		} catch (Exception e) {
			throw e;
		} finally {
			closeSession(session);
		}
	}
}
